package org.abratuhi.kvngroupware.pubsub;

import java.util.Date;

public class Publication {
	
	String publicationId;
	String publisherId;
	String[] attributes;
	Value[] values;
	long creationDate;
	long expirationDate;
	
	public Publication(String publicatorId, String[] attributes, Value[] values, long ttl){
		this.publisherId = publicatorId;
		this.attributes = attributes;
		this.values = values;
		this.creationDate = new Date().getTime();
		this.expirationDate = creationDate + ttl;
		this.publicationId = publicatorId + creationDate;
	}
	
	public int indexOf(String attribute){
		for(int i=0; i<attributes.length; i++){
			if(attributes[i].equals(attribute)){
				return i;
			}
		}
		return -1;
	}
	
	public Value valueOf(String attribute){
		return (indexOf(attribute) != 1)? values[indexOf(attribute)] : null;
	}
	
	public String toString(){
		String result = publicationId + ": " + publisherId + "; ";
		for(int i=0; i<attributes.length; i++) result += attributes[i] + " = " + values[i].toString() + ", ";
		result += ";";
		return result;
	}

}
